package com.longrise.common.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.design.widget.Snackbar;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.longrise.android.listview_library_refrush.PullToRefreshListView;
import com.longrise.common.R;
import com.longrise.common.widget.WaitDialog;

import java.text.SimpleDateFormat;
import java.util.Date;

public final class DZZWTools {

    private static WaitDialog mWaitDialog;
    private static DownLoadDetailDialog mNotBackWaitDialog;

    private static Toast mToast;

    private DZZWTools() {
    }

    /**
     * 推荐使用
     *
     * @param msg     toast的信息
     * @param timeOut 延迟时间
     */
    public static void showToast(String msg, int timeOut) {
        showToast(null, msg, timeOut);
    }

    /**
     * 推荐使用
     * 弹出正在加载...等待对话框，推荐首先考虑在xml中<include layout="@layout/byjk_loading_dialog"/>
     *
     * @param cxt activity
     */
    public static void showWaitDialog(Context cxt) {
        if (notAlive(cxt)) {
            return;
        }
        createDialog(cxt);
        try {
            mWaitDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 推荐使用
     */
    public static void dismissDialog() {
        dismissDialog(null);
    }

    /**
     * 兼容较早使用情况
     *
     * @param cxt     上下文，可以传递null
     * @param msg     toast 的msg
     * @param timeOut 延迟时间
     *                在当前App中只有一个Toast实例，toast相关信息
     */
    public static void showToast(Context cxt, String msg, int timeOut) {
        try {
            if (TextUtils.isEmpty(msg) || cxt == null) {
                return;
            }
            if (timeOut <= 0) {
                timeOut = Toast.LENGTH_SHORT;
            }
            View v = View.inflate(AppUtil.getContext(), R.layout.toast_view, null);
            TextView tv = (TextView) v.findViewById(R.id.toast_tv);
            tv.setText(msg);
            if (mToast == null) {
                mToast = new Toast(AppUtil.getContext());
            }
            mToast.setView(v);
            mToast.setDuration(timeOut);
            mToast.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void showSnackbar(Context cxt, String msg, int time) {
        if (cxt instanceof Activity && !TextUtils.isEmpty(msg)) {
            if (time < 0) {
                time = Toast.LENGTH_SHORT;
            }
            Snackbar.make(((Activity) cxt).getWindow().getDecorView(), msg, time).show();
        }
    }

    private static boolean notAlive(Context cxt) {
        if (cxt == null || !(cxt instanceof Activity)) {
            return true;
        }
        Activity act = (Activity) cxt;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return act.isDestroyed() || act.isFinishing();
        }
        return act.isFinishing();
    }

    /**
     * 创建新的dialog都要将之前的置为null
     */
    private static void createDialog(Context cxt) {
        dismissDialog(null);
        if (mWaitDialog == null) {
            synchronized (DZZWTools.class) {
                if (mWaitDialog == null) {
                    mWaitDialog = new WaitDialog(cxt);
                }
            }
        }
    }

    /**
     * 兼容较早版本使用
     * 如果未用到PullToReFresh则传递null
     */
    public static void dismissDialog(final PullToRefreshListView listview) {
        try {
            if (mWaitDialog != null) {
                mWaitDialog.dismiss();
            }
            if (listview != null) {
                listview.onRefreshComplete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //必须设置为null
        mWaitDialog = null;
    }

    /**
     * 有时候只想show一个dialog
     */
    public static void showWaitDialog2(Context cxt) {
        if (mWaitDialog != null && mWaitDialog.isShowing()) {
            return;
        }
        dismissDialog2();
        if (mWaitDialog == null) {
            synchronized (DZZWTools.class) {
                if (mWaitDialog == null) {
                    mWaitDialog = new WaitDialog(cxt);
                }
            }
        }
        try {
            mWaitDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * xkj 有时候弹窗dismiss不彻底会导致很多问题 需要置为空
     */
    public static void dismissDialog2() {
        if (mWaitDialog != null) {
            mWaitDialog.dismiss();
            mWaitDialog = null;
        }
    }

    /**
     * 显示不能返回dialog
     */
    public static void showNotBackWaitDialog(Context cxt) {
        try {
            if (mNotBackWaitDialog == null) {
                synchronized (DZZWTools.class) {
                    if (mNotBackWaitDialog == null) {
                        mNotBackWaitDialog = new DownLoadDetailDialog(cxt);
                    }
                }
            }
            if (!mNotBackWaitDialog.isShowing()) {
                mNotBackWaitDialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏不能返回的dialog
     */
    public static void dismissNotBackDialog() {
        if (mNotBackWaitDialog != null && mNotBackWaitDialog.isShowing()) {
            mNotBackWaitDialog.dismiss();
            mNotBackWaitDialog = null;
        }
    }

    public static void hideSoftInput(Activity cxt) {
        try {
            InputMethodManager manager = (InputMethodManager) cxt.getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (manager.isActive()) {
                manager.hideSoftInputFromWindow(cxt.getCurrentFocus().getWindowToken(), 0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void showSoftInput(Context cxt) {
        try {
            InputMethodManager manager = (InputMethodManager) cxt.getSystemService(Context.INPUT_METHOD_SERVICE);
            if (!manager.isActive()) {
                manager.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getSystemTime(String timeType) {
        SimpleDateFormat format = new SimpleDateFormat(timeType);
        Date date = new Date();
        return format.format(date);
    }
}
